#!/bin/sh

REMOTEHOST=''
REMOTEDIR='.'

CONFIGURE=true
BUILD=true
TEST=true
PORT=''
REV='HEAD'

SSH_OPTS=''

while [ "$1" != '' ]
do
 case "$1" in
  '-h'|'--help')
  echo "Usage: $0 [OPTIONS] host..."
  echo
cat <<HELP
 -h --help                        - Show this help
    --remote-dir=DIR              - Set dir on remote site
    --build={true|false}          - build on remote site?
    --configure={true,false}      - configure on remote site?
    --test={true,false}           - test on remote site?
    --port=PORT                   - Set port of remote site
    --rev=REVISION                - Set the revision (or tag) to send
HELP
   exit 0;
  ;;
  '--remote-dir='*)
   REMOTEDIR=`echo "$1" | cut -d= -f2`
  ;;
  '--build='*)
   BUILD=`echo $1 | cut -d= -f2`
  ;;
  '--configure='*)
   CONFIGURE=`echo $1 | cut -d= -f2`
  ;;
  '--test='*)
   TEST=`echo $1 | cut -d= -f2`
  ;;
  '--port='*)
   PORT=`echo $1 | cut -d= -f2`
  ;;
  '--rev='*)
   REV=`echo $1 | cut -d= -f2`
  ;;
  *)
    REMOTEHOST="$1"
  ;;
 esac
 shift
done

if [ "$PORT" != '' ]
then
 SSH_OPTS="-p $PORT $SSH_OPTS"
fi

SSH_OPTS="$SSH_OPTS $REMOTEHOST"

$CONFIGURE || BUILD=false
$BUILD     || TEST=false

LOCALDIR="sra-@$REMOTEHOST"

mkdir $LOCALDIR

cd $LOCALDIR
cvs co -r $REV roaraudio
cd ..

{
 echo '#!/bin/sh'
 echo

 echo 'MAKE=`which gmake make 2> /dev/null | grep ^/ | head -n 1`'

 echo 'cd roaraudio'

 $CONFIGURE && echo './configure || exit 1'
 $BUILD     && echo '$MAKE       || exit 2'
 $TEST      && echo '$MAKE test  || exit 3'

 echo 'exit 0'
 echo '#ll'
} > $LOCALDIR/setup
chmod +rx $LOCALDIR/setup

tar -czf - $LOCALDIR | ssh $SSH_OPTS "cd $REMOTEDIR && rm -rf $LOCALDIR && gunzip | tar -xvf - && cd $LOCALDIR && ./setup"

rm -rf $LOCALDIR

#ll
